package com.seagox.chat.chat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seagox.chat.chat.entity.Calendar;
import com.seagox.chat.chat.mapper.CalendarMapper;
import com.seagox.chat.chat.service.ICalendarService;
import com.seagox.chat.common.ResultData;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CalendaService implements ICalendarService {

	@Autowired
	private CalendarMapper calendarMapper;

	@Override
	public ResultData queryByDate(String userId, String day) {
		List<Map<String, Object>> list = calendarMapper.queryByDate(userId, day);
		JSONObject result = new JSONObject();
		for (Map<String, Object> calendar : list) {
			JSONArray dayArray = new JSONArray();
			if (result.containsKey(calendar.get("day").toString())) {
				dayArray = result.getJSONArray(calendar.get("day").toString());
			}
			dayArray.add(calendar);
			result.put(calendar.get("day").toString(), dayArray);
		}
		return ResultData.success(result);
	}

	@Override
	public ResultData insert(Calendar calendar) {
		calendarMapper.insert(calendar);
		return ResultData.success(calendar.getId());
	}

	@Override
	public ResultData update(Calendar calendar) {
		calendarMapper.updateById(calendar);
		return ResultData.success(null);
	}

	@Override
	public ResultData delete(String id) {
		calendarMapper.deleteById(id);
		return ResultData.success(null);
	}

}
